package org.xdq.demo.emp.api;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.xdq.demo.base.*;
import org.xdq.demo.emp.dto.EmpDto;
import org.xdq.demo.emp.model.Employee;
import org.xdq.demo.emp.service.EmpService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j //lombok输出日志注解（使用slf4j日志工具）
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping("/")
    public Result empList(EmpDto empDto){
//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        try {
            List<Employee> empList =  empService.getEmpList(empDto);
            return Result.OK(empList);
        } catch (SysException e) {
            //e.printStackTrace();
            log.error("系统错误！", e);//打印错误日志，给研发人员看的
            return Result.err(Result.CODE_ERR_SYS,"系统升级中......");
        } catch (BusinessException e) {
            return Result.err(Result.CODE_ERR_BUSINESS, e.getMessage());
        }
    }

    @GetMapping("/one")
    public Result getEmp(String eid){
        Employee emp = empService.getEmp(eid);
        return Result.OK(emp);
    }


    @GetMapping("/{id}")
    public Result getEmp2(@PathVariable("id") String eid){
        //@PathVariable("id")表示将路径变量id对应的值赋给参数eid
        Employee emp = empService.getEmp(eid);
        return Result.OK(emp);
    }
    //@RequestBody表示参数形式是请求体（payload）
    @PostMapping("/")
    public Result addEmp(@RequestBody EmpDto empDto){
        try {


            empService.addOneEmp(empDto);
            return Result.OK();

        } catch (SysException e) {
            //e.printStackTrace();
            log.error("系统错误！", e);//打印错误日志，给研发人员看的
            return Result.err(Result.CODE_ERR_SYS,"系统升级中......");
        } catch (BusinessException e) {
            return Result.err(Result.CODE_ERR_BUSINESS, e.getMessage());
        }
    }

    @PutMapping("/")
    public Result updEmp(@RequestBody EmpDto empDto){
        try {
            empService.updateEmp(empDto);
            return Result.OK();

        } catch (SysException e) {
            //e.printStackTrace();
            log.error("系统错误！", e);//打印错误日志，给研发人员看的
            return Result.err(Result.CODE_ERR_SYS,"系统升级中......");
        } catch (BusinessException e) {
            return Result.err(Result.CODE_ERR_BUSINESS, e.getMessage());
        }
    }

    @DeleteMapping("/")
    public Result multiDelEmp(@RequestBody String[] eids){
        try {
            empService.deleteEmp(eids);
            return Result.OK();

        } catch (SysException e) {
            //e.printStackTrace();
            log.error("系统错误！", e);//打印错误日志，给研发人员看的
            return Result.err(Result.CODE_ERR_SYS,"系统升级中......");
        } catch (BusinessException e) {
            return Result.err(Result.CODE_ERR_BUSINESS, e.getMessage());
        }
    }
    //获得当前登录用户
    @GetMapping("/curruser")
    public Result getCurrentUser(HttpServletRequest request){
        try {
            String clientToken = request.getHeader(Constants.HEADER_TOKEN);

            //验证
            //TokenUtils.verify(clientToken);

            CurrentUser currentUser = TokenUtils.getCurrentUser(clientToken);

            return Result.OK(currentUser);
        } catch (BusinessException e) {
            return Result.err(Result.CODE_ERR_BUSINESS, e.getMessage());
        } catch (SysException e){
            log.error("系统错误！", e);//打印错误日志，给研发人员看的
            return Result.err(Result.CODE_ERR_SYS,"系统升级中......");
        }
    }


}
